package com.jh.paymentchannel.util.yipiaolian;

import cn.epaylinks.common.HttpTLSUtil;
import cn.epaylinks.common.HttpUtil;
import cn.epaylinks.common.PayCenterDataException;
import cn.epaylinks.common.sign.SignTool;
import cn.epaylinks.service.request.PayRequest;
import cn.epaylinks.service.request.QueryOrderRequest;
import cn.epaylinks.service.request.RefundQueryRequest;
import cn.epaylinks.service.request.RefundRequest;
import cn.epaylinks.service.response.PayResponse;
import cn.epaylinks.service.response.QueryOrderResponse;
import cn.epaylinks.service.response.RefundQueryResponse;
import cn.epaylinks.service.response.RefundResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PaycenterService
{
  private static final Logger logger = LoggerFactory.getLogger(PaycenterService.class);
  private static PaycenterService paycenterService = new PaycenterService();

  public static PaycenterService getInstance()
  {
    return paycenterService;
  }

  public void paycenterPay(PayRequest payRequest, HttpServletResponse response)
    throws Exception
  {
    String url = Parameters.getProperty("pay_url");
    String requestStr = payRequest.getPayRequestStr();
    String httpUrl = url + "?" + requestStr;
    response.sendRedirect(httpUrl);
  }

  public PayResponse paycenterPayReturn(HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    PayResponse payResponse = new PayResponse(request, response);

    if (SignTool.verifySign(payResponse.getParameters())) {
      logger.info("验证签名成功");
      System.out.println("验证签名成功");
      return payResponse;
    }
    logger.error("签名验证失败");
    throw new PayCenterDataException("签名验证失败");
  }

  public PayResponse paycenterPayNotifyReturn(HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    PayResponse payResponse = new PayResponse(request, response);

    if (SignTool.verifySign(payResponse.getParameters())) {
      logger.info("验证签名成功");
      System.out.println("验证签名成功");
      if ("1".equals(payResponse.getParameter("pay_result"))) {
        logger.info("返回处理结果为：支付成功");
      }
      else
      {
        logger.warn("返回处理结果为：支付失败");
      }
      return payResponse;
    }
    logger.error("签名验证失败");
    throw new PayCenterDataException("签名验证失败");
  }

  public QueryOrderResponse paycenterQueryOrder(QueryOrderRequest queryOrder)
    throws Exception
  {
    String url = Parameters.getProperty("order_url");
    queryOrder.setPartner(Parameters.getProperty("partner"));
    logger.info("===============订单查询开始===============");
    String requestStr = queryOrder.getQueryOrderStr();
    String httpUrl = url + "?" + requestStr;
    logger.info("请求url:" + httpUrl);
    String responseStr = "";
    if (url.startsWith("https"))
      responseStr = HttpTLSUtil.execu(url, requestStr, "ISO-8859-1", "GBK");
    else {
      responseStr = HttpUtil.sendPost(url, requestStr);
    }
    logger.info("收到响应内容：" + responseStr);
    QueryOrderResponse queryOrderResponse = null;
    try {
      queryOrderResponse = QueryOrderResponse.xmlToBean(responseStr);
    }
    catch (PayCenterDataException e) {
      e.printStackTrace();
      logger.error(e.getMessage());
    }
    logger.info("===============订单查询结束===============");
    return queryOrderResponse;
  }

  public RefundResponse paycenterRefund(RefundRequest refundRequest)
    throws Exception
  {
    String url = Parameters.getProperty("order_url");
    refundRequest.setPartner(Parameters.getProperty("partner"));
    logger.info("===============退款请求开始===============");
    String requestStr = refundRequest.getRefundRequestStr();
    String httpUrl = url + "?" + requestStr;
    logger.info("请求url:" + httpUrl);
    String responseStr = "";
    if (url.startsWith("https"))
      responseStr = HttpTLSUtil.execu(url, requestStr, "ISO-8859-1", "GBK");
    else {
      responseStr = HttpUtil.sendPost(url, requestStr);
    }
    logger.info("收到响应内容：" + responseStr);
    RefundResponse refundResponse = null;
    try {
      refundResponse = RefundResponse.xmlToBean(responseStr);
    }
    catch (PayCenterDataException e) {
      e.printStackTrace();
      logger.error(e.getMessage());
    }
    logger.info("===============退款请求结束===============");
    return refundResponse;
  }

  public RefundQueryResponse paycenterRefundQuery(RefundQueryRequest refundQuery)
    throws Exception
  {
    String url = Parameters.getProperty("order_url");
    refundQuery.setPartner(Parameters.getProperty("partner"));
    logger.info("===============退款查询开始===============");
    String requestStr = refundQuery.getRefundQueryStr();
    String httpUrl = url + "?" + requestStr;
    logger.info("请求url:" + httpUrl);
    String responseStr = "";
    if (url.startsWith("https"))
      responseStr = HttpTLSUtil.execu(url, requestStr, "ISO-8859-1", "GBK");
    else {
      responseStr = HttpUtil.sendPost(url, requestStr);
    }
    logger.info("收到响应内容：" + responseStr);
    RefundQueryResponse refundQueryResponse = null;
    try {
      refundQueryResponse = RefundQueryResponse.xmlToBean(responseStr);
    }
    catch (PayCenterDataException e) {
      e.printStackTrace();
      logger.error(e.getMessage());
    }
    logger.info("===============退款查询结束===============");
    return refundQueryResponse;
  }

//  public static void main(String[] args)
//  {
//    try
//    {
//      QueryOrderRequest queryOrder = new QueryOrderRequest();
//      queryOrder.setOutTradeNo("1447208747438");
//      queryOrder.setSignType("SHA256withRSA");
//      queryOrder.setCertId(SecurityUtil.certId);
//      queryOrder.setVersion("4.0");
//      QueryOrderResponse queryOrderResponse = paycenterService.paycenterQueryOrder(queryOrder);
//      System.out.println("订单查询响应码:" + queryOrderResponse.getRespCode());
//      System.out.println("订单总金额:" + queryOrderResponse.getAmount());
//      System.out.println("订单流水号:" + queryOrderResponse.getOutTradeNo());
//
//      RefundRequest refundRequest = new RefundRequest("1474353158522", "TK1447208747432", "0.01", "0.01");
//      refundRequest.setSignType("SHA256withRSA");
//      refundRequest.setCertId(SecurityUtil.certId);
//      refundRequest.setVersion("4.0");
//      RefundResponse refundResponse = paycenterService.paycenterRefund(refundRequest);
//      System.out.println("退款结果响应:" + refundResponse.getRespDesc());
//      System.out.println("易票联退款流水号：" + refundResponse.getRefundId());
//
//      PaycenterService paycenterService = getInstance();
//      RefundQueryRequest refundQuery = new RefundQueryRequest("1474353158522", "1939", "TK1447208747438");
//      refundQuery.setSignType("SHA256withRSA");
//      refundQuery.setCertId(SecurityUtil.certId);
//      refundQuery.setVersion("4.0");
//      RefundQueryResponse refundQueryResponse = paycenterService.paycenterRefundQuery(refundQuery);
//      System.out.println("退款查询结果响应码：" + refundQueryResponse.getRespCode());
//    }
//    catch (Exception e)
//    {
//      e.printStackTrace();
//    }
//  }
}
